package logic

import (
	"context"
	"fmt"
	"mini/model/mysql"
	"mini/model/redis"

	redis2 "github.com/go-redis/redis"
	"github.com/zeromicro/go-zero/core/logx"
	"mini/minirpc/internal/svc"
	"mini/minirpc/minirpc"
)

type ProductLikeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductLikeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductLikeLogic {
	return &ProductLikeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductLikeLogic) ProductLike(in *minirpc.ProductLikeRequest) (*minirpc.ProductLikeResponse, error) {
	// todo: add your logic here and delete this line
	var project mysql.Products
	mysql.DB.Debug().Where("id=?", in.Id).First(&project)
	i := project.Like + 1
	projects := mysql.Products{
		Like: i,
	}
	err := mysql.DB.Debug().Where("id=?", in.Id).Updates(&projects).Error
	if err != nil {
		return nil, fmt.Errorf("点赞失败")
	}
	redis.Rdb.ZAdd("ProjectLike", redis2.Z{
		Score:  float64(projects.Like),
		Member: project.ID,
	})
	return &minirpc.ProductLikeResponse{}, nil
}
